package com.itheima.controller;

import com.itheima.entity.AliOSSProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description UploadController
 * @Author 你的名字拼接或昵称
 * @Date 2025-09-04
 */
@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {
    @Autowired
    private  AliOSSProperties aliOSSProperties;


    @PostMapping
    public Result upload(MultipartFile file) throws Exception{
        String originalFilename = file.getOriginalFilename();
        log.info("原始文件名"+originalFilename);
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url= AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(),aliOSSProperties.getBucket(),file.getBytes(),extName);


        return Result.success(url);
    }
}
